package com.base.system.managedBean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.context.FacesContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.operamasks.faces.annotation.Action;
import org.operamasks.faces.annotation.BeforeRender;
import org.operamasks.faces.annotation.Bind;
import org.operamasks.faces.annotation.DataModel;
import org.operamasks.faces.annotation.EventListener;
import org.operamasks.faces.annotation.Inject;
import org.operamasks.faces.annotation.ManagedBean;
import org.operamasks.faces.annotation.ManagedBeanScope;
import org.operamasks.faces.annotation.SaveState;
import org.operamasks.faces.component.grid.impl.UIDataGrid;
import org.operamasks.faces.user.util.Browser;

import com.base.commons.SpringBeanHolder;
import com.base.security.SecurityConst;
import com.base.security.dao.ISecurityUserDao;
import com.base.security.entity.SecurityUser;
import com.base.security.service.SecurityEntityManager;

/**
 * 
 * This managed bean is generated automatically
 */
@ManagedBean(name = "system_securityUserListBean", scope = ManagedBeanScope.REQUEST)
public class SecurityUserListBean implements Serializable {
	private final static Log logger = LogFactory
			.getLog(SecurityUserListBean.class);
	protected SecurityEntityManager securityEntityManager = SpringBeanHolder
			.getBean("securityEntityManager");
	protected ISecurityUserDao securityUserDAO = SpringBeanHolder
			.getBean("securityUserDao");
	@Inject
	private FacesContext context;
	@SaveState
	@Bind(id = "keywords", attribute = "value")
	private String keywords;
	// @Inject
	// public EventBroadcaster event;

	@Bind(id = "securityUserGrid", attribute = "binding")
	private UIDataGrid securityUserGrid;

	@SuppressWarnings("unchecked")
	@DataModel(id = "securityUserGrid")
	private List<SecurityUser> getUsers() {
		if (null != keywords && !"".equals(keywords)) {
			List<SecurityUser> searchUsers = new ArrayList<SecurityUser>();
			String hql = "from SecurityUser as user where user.fullname like '%"
					+ keywords + "%' order by user.fullname";
			searchUsers = securityUserDAO.findByHql(hql);
			return searchUsers;
		}
		return securityUserDAO.getAll();
	}

	@BeforeRender
	public void beforeRender(boolean isPostback) {
		logger.debug("Listbean 中的 postBack:" + isPostback);
		if (!isPostback) {
			keywords = null;
		}
	}

	@Action(id = "query")
	public void query() {
		if (null != keywords && !"".equals(keywords)) {
			// this.data = securityEntityManager.getUsersOfFullname(keywords);
			securityUserGrid.reload();
		}
	}

	@Action(id = "reset")
	public void reset() {
		keywords = null;
		// data = securityUserDAO.getAll();
		securityUserGrid.reload();
	}

	@Action(id = "createSecurityUser")
	public void createSecurityUser() {
		// System.out.println("调用新建方法");
		String url = "/modules/system/addSecurityUser.faces";
		url = context.getExternalContext().getRequestContextPath() + url;
		String script = "addCenterLayoutTab('" + SecurityConst.ADD_TAB + "','"
				+ "新建用户" + "', '" + url + "','" + true + "','refreshData')";
		Browser.execClientScript("window.top", script);
	}

	@Action(id = "editSecurityUser")
	public void editSecurityUser() {
		if (initSelected()) {
			String url = "/modules/system/editSecurityUser.faces";
			url = context.getExternalContext().getRequestContextPath() + url;
			String script = "addCenterLayoutTab('" + SecurityConst.EDIT_TAB
					+ "','" + "编辑用户" + "', '" + url + "')";
			Browser.execClientScript("window.top", script);
		}
	}

	private boolean initSelected() {
		Object[] objs = securityUserGrid.getSelectedValues();
		// System.out.println("取得的选取值是：" + objs);
		if (objs == null || objs.length == 0) {
			alertNoRecordSelected();
			return false;
		}
		context.getExternalContext().getSessionMap().put(
				SecurityConst.SELECT_ROW_DATA, (SecurityUser) objs[0]);
		return true;
	}

	private void alertNoRecordSelected() {
		Browser.execClientScript("window.alert('请选择一条记录！');");
	}

	@Action(id = "deleteSecurityUser")
	public void deleteSecurityUser() {
		Object[] objs = securityUserGrid.getSelectedValues();
		if (objs == null || objs.length == 0) {
			return;
		}
		securityUserDAO.delete((SecurityUser) objs[0]);
		// data.remove((SecurityUser) objs[0]);
		securityUserGrid.reload();
		// Browser.execClientScript("window.alert('删除成功！');");
	}

	public void showSecurityUser() {
		if (initSelected()) {
			String url = "/modules/system/showSecurityUser.faces";
			url = context.getExternalContext().getRequestContextPath() + url;
			String script = "addCenterLayoutTab('" + SecurityConst.SHOW_TAB
					+ "','" + "查看用户" + "', '" + url + "')";
			Browser.execClientScript("window.top", script);
		}
	}

	@EventListener("deleteSecurityUser")
	private void deleteSecurityUserListener(SecurityUser securityUser) {
		// data.remove(securityUser);
	}

	@EventListener("addSecurityUser")
	private void addSecurityUserListener(SecurityUser securityUser) {
		// logger.debug("执行了几次呢？");
		// data.add(securityUser);
	}
}
